<?php
declare (strict_types = 1);

namespace app\controller;

use app\BaseController;
use app\model\T_CO_ITEM;
use app\model\T_CO_SUPPLIER;
use app\model\T_QM_INSPECT_INFO;
use app\model\T_QM_SAMPLE_CONFIG;
use think\Db;
use think\facade\View;
use think\Request;

class Qualitykb extends BaseController
{
    /**
     * 显示IQC段看板
     *
     * @return \think\Response
     */
    public function index()
    {
        //待检数量总个数和单据数
        $sumNum=(new T_QM_INSPECT_INFO())
            ->where('TEST_STEP','=',1)
            ->where('STATUS','=',2)->field('id')->count();
        //单据数
        $data1['sumNum']=$sumNum;
        $data1['CodeNum']=$sumNum;
        //当前待检任务单
        $res1=(new T_QM_INSPECT_INFO())->alias('a')
            ->leftJoin('T_CO_ITEM b','a.ITEM_CODE=b.ITEM_CODE')
            ->leftJoin('T_CO_SUPPLIER c','a.SUPPLIER_CODE=c.SUPPLIER_CODE')
            ->field('a.DOC_NO,a.IS_URGENT,c.SUPPLIER_NAME,a.SEND_QTY,a.SEND_END_TIME,a.TEST_START_TIME,a.ITEM_CODE,b.ITEM_NAME')
            ->where('TEST_STEP','=',1)->where('STATUS','=',2)
            ->select();
        $data1['data']=$res1->toArray();
        foreach ($data1['data'] as $k=>$v){
            //是否加急
            if($v['IS_URGENT']==1){
                $data1['data'][$k]['IS_URGENT']='是';
            }else{
                $data1['data'][$k]['IS_URGENT']='否';
            }
        }

        //今日已检数量总个数和单据数
        $sumNum2=(new T_QM_INSPECT_INFO())
            ->where('TEST_STEP','=',1)
            ->where('STATUS','>=',4)
            ->where([['TEST_START_TIME','>=',date('Y-m-d 00:00:00')],['TEST_START_TIME','<=',date('Y-m-d 23:59:59')]])
            ->where([['TEST_END_TIME','>=',date('Y-m-d 00:00:00')],['TEST_END_TIME','<=',date('Y-m-d 23:59:59')]])
            ->field('id')->count();
        $data2['sumNum']=$sumNum2;
        $data2['CodeNum']=$sumNum2;
        //今日已检任务单
        $res2=(new T_QM_INSPECT_INFO())->alias('a')
            ->leftJoin('T_CO_ITEM b','a.ITEM_CODE=b.ITEM_CODE')
            ->leftJoin('T_CO_SUPPLIER c','a.SUPPLIER_CODE=c.SUPPLIER_CODE')
            ->where('TEST_STEP','=',1)
            ->where('STATUS','>=',4)
            ->where([['TEST_START_TIME','>=',date('Y-m-d 00:00:00')],['TEST_START_TIME','<=',date('Y-m-d 23:59:59')]])
            ->where([['TEST_END_TIME','>=',date('Y-m-d 00:00:00')],['TEST_END_TIME','<=',date('Y-m-d 23:59:59')]])
            ->field('a.DOC_NO,a.IS_URGENT,a.ITEM_CODE,b.ITEM_NAME,c.SUPPLIER_NAME,a.SEND_QTY,a.PLAN_QTY,a.REAL_QTY,a.ERROR_QTY,a.SEND_END_TIME,a.TEST_START_TIME')
            ->select();
        $data2['data']=$res2->toArray();
        foreach ($data2['data'] as $k=>$v){
            //是否加急
            if($v['IS_URGENT']==1){
                $data2['data'][$k]['IS_URGENT']='是';
            }else{
                $data2['data'][$k]['IS_URGENT']='否';
            }
        }
        //今日不良数据TEST_RESULT
        $info2=(new T_QM_INSPECT_INFO())->alias('a')
            ->leftJoin('T_CO_ITEM b','a.ITEM_CODE=b.ITEM_CODE')
            ->leftJoin('T_CO_SUPPLIER c','a.SUPPLIER_CODE=c.SUPPLIER_CODE')
            ->where('TEST_STEP','=',1)
            ->where('STATUS','>=',4)
            ->where([['TEST_START_TIME','>=',date('Y-m-d 00:00:00')],['TEST_START_TIME','<=',date('Y-m-d 23:59:59')]])
            ->where([['TEST_END_TIME','>=',date('Y-m-d 00:00:00')],['TEST_END_TIME','<=',date('Y-m-d 23:59:59')]])
            ->where('TEST_RESULT','=',2)
            ->field('a.DOC_NO,a.IS_URGENT,a.ITEM_CODE,b.ITEM_NAME,c.SUPPLIER_NAME,a.SEND_QTY,a.PLAN_QTY,a.REAL_QTY,a.ERROR_QTY,a.SEND_END_TIME,a.TEST_START_TIME')
            ->select();
        $data2['NG']=$info2->toArray();
        $data2['NgSumNum']=count($data2['NG']);
        foreach ($data2['NG'] as $k=>$v){
            //是否加急
            if($v['IS_URGENT']==1){
                $data2['NG'][$k]['IS_URGENT']='是';
            }else{
                $data2['NG'][$k]['IS_URGENT']='否';
            }
        }

        //昨日检验数据
        $sumNum3=(new T_QM_INSPECT_INFO())
            ->where('TEST_STEP','=',1)
            ->where('STATUS','>=',4)
            ->where([['TEST_START_TIME','>=',date('Y-m-d 00:00:00',strtotime('-1 day'))],['TEST_START_TIME','<=',date('Y-m-d 23:59:59',strtotime('-1 day'))]])
            ->where([['TEST_END_TIME','>=',date('Y-m-d 00:00:00',strtotime('-1 day'))],['TEST_END_TIME','<=',date('Y-m-d 23:59:59',strtotime('-1 day'))]])
            ->field('id')->count();
        $data3['sumNum']=$sumNum3;
        $data3['CodeNum']=$sumNum3;
        //昨日不良数据总个数和单据数
        $NgNum=(new T_QM_INSPECT_INFO())
            ->where('TEST_STEP','=',1)
            ->where('STATUS','>=',4)
            ->where([['TEST_START_TIME','>=',date('Y-m-d 00:00:00',strtotime('-1 day'))],['TEST_START_TIME','<=',date('Y-m-d 23:59:59',strtotime('-1 day'))]])
            ->where([['TEST_END_TIME','>=',date('Y-m-d 00:00:00',strtotime('-1 day'))],['TEST_END_TIME','<=',date('Y-m-d 23:59:59',strtotime('-1 day'))]])
            ->where('TEST_RESULT','=',2)
            ->field('id')->count();
        $data3['NgSumNum']=$NgNum;
        $data3['NgCodeNum']=$NgNum;
        if($this->request->isAjax()){
           return json(['data1'=>$data1,'data2'=>$data2,'data3'=>$data3],200);
        }
        return view('/IQC',['data1'=>$data1,'data2'=>$data2,'data3'=>$data3]);
    }

    /**
     * 显示FQC段看板
     *
     * @return \think\Response
     */
    public function fqckb()
    {
        //待检数量总个数和单据数
        $sumNum=(new T_QM_INSPECT_INFO())
            ->where('TEST_STEP','=',2)
            ->where('STATUS','=',2)->field('id')->count();
        //单据数
        $data1['sumNum']=$sumNum;
        $data1['CodeNum']=$sumNum;
        //当前待检任务单
        $res1=(new T_QM_INSPECT_INFO())->alias('a')
            ->leftJoin('T_CO_ITEM b','a.ITEM_CODE=b.ITEM_CODE')
            ->leftJoin('T_CO_SUPPLIER c','a.SUPPLIER_CODE=c.SUPPLIER_CODE')
            ->where('TEST_STEP','=',2)->where('STATUS','=',2)
            ->field('a.DOC_NO,a.IS_URGENT,c.SUPPLIER_NAME,a.SEND_QTY,a.SEND_END_TIME,a.TEST_START_TIME,a.ITEM_CODE,b.ITEM_NAME')
            ->select();
        $data1['data']=$res1->toArray();
        foreach ($data1['data'] as $k=>$v){
            //是否加急
            if($v['IS_URGENT']==1){
                $data1['data'][$k]['IS_URGENT']='是';
            }else{
                $data1['data'][$k]['IS_URGENT']='否';
            }
        }

        //今日已检数量总个数和单据数
        $sumNum2=(new T_QM_INSPECT_INFO())
            ->where('TEST_STEP','=',2)
            ->where('STATUS','>=',4)
            ->where([['TEST_START_TIME','>=',date('Y-m-d 00:00:00')],['TEST_START_TIME','<=',date('Y-m-d 23:59:59')]])
            ->where([['TEST_END_TIME','>=',date('Y-m-d 00:00:00')],['TEST_END_TIME','<=',date('Y-m-d 23:59:59')]])
            ->field('id')->count();
        $data2['sumNum']=$sumNum2;
        $data2['CodeNum']=$sumNum2;
        //今日已检任务单
        $res2=(new T_QM_INSPECT_INFO())->alias('a')
            ->leftJoin('T_CO_ITEM b','a.ITEM_CODE=b.ITEM_CODE')
            ->leftJoin('T_CO_SUPPLIER c','a.SUPPLIER_CODE=c.SUPPLIER_CODE')
            ->where('TEST_STEP','=',2)
            ->where('STATUS','>=',4)
            ->where([['TEST_START_TIME','>=',date('Y-m-d 00:00:00')],['TEST_START_TIME','<=',date('Y-m-d 23:59:59')]])
            ->where([['TEST_END_TIME','>=',date('Y-m-d 00:00:00')],['TEST_END_TIME','<=',date('Y-m-d 23:59:59')]])
            ->field('a.DOC_NO,a.IS_URGENT,a.ITEM_CODE,b.ITEM_NAME,c.SUPPLIER_NAME,a.SEND_QTY,a.PLAN_QTY,a.REAL_QTY,a.ERROR_QTY,a.SEND_END_TIME,a.TEST_START_TIME')
            ->select();
        $data2['data']=$res2->toArray();
        foreach ($data2['data'] as $k=>$v){
            //是否加急
            if($v['IS_URGENT']==1){
                $data2['data'][$k]['IS_URGENT']='是';
            }else{
                $data2['data'][$k]['IS_URGENT']='否';
            }
        }
        //今日不良数据TEST_RESULT
        $info2=(new T_QM_INSPECT_INFO())->alias('a')
            ->leftJoin('T_CO_ITEM b','a.ITEM_CODE=b.ITEM_CODE')
            ->leftJoin('T_CO_SUPPLIER c','a.SUPPLIER_CODE=c.SUPPLIER_CODE')
            ->where('TEST_STEP','=',2)
            ->where('STATUS','>=',4)
            ->where([['TEST_START_TIME','>=',date('Y-m-d 00:00:00')],['TEST_START_TIME','<=',date('Y-m-d 23:59:59')]])
            ->where([['TEST_END_TIME','>=',date('Y-m-d 00:00:00')],['TEST_END_TIME','<=',date('Y-m-d 23:59:59')]])
            ->where('TEST_RESULT','=',2)
            ->field('a.DOC_NO,a.IS_URGENT,a.ITEM_CODE,b.ITEM_NAME,c.SUPPLIER_NAME,a.SEND_QTY,a.PLAN_QTY,a.REAL_QTY,a.ERROR_QTY,a.SEND_END_TIME,a.TEST_START_TIME')
            ->select();
        $data2['NG']=$info2->toArray();
        $data2['NgSumNum']=count($info2);
        foreach ($data2['NG'] as $k=>$v){
            //是否加急
            if($v['IS_URGENT']==1){
                $data2['NG'][$k]['IS_URGENT']='是';
            }else{
                $data2['NG'][$k]['IS_URGENT']='否';
            }
        }

        //昨日检验数据数量
        $sumNum3=(new T_QM_INSPECT_INFO())
            ->where('TEST_STEP','=',2)
            ->where('STATUS','>=',4)
            ->where([['TEST_START_TIME','>=',date('Y-m-d 00:00:00',strtotime('-1 day'))],['TEST_START_TIME','<=',date('Y-m-d 23:59:59',strtotime('-1 day'))]])
            ->where([['TEST_END_TIME','>=',date('Y-m-d 00:00:00',strtotime('-1 day'))],['TEST_END_TIME','<=',date('Y-m-d 23:59:59',strtotime('-1 day'))]])
            ->field('id')->count();
        $data3['sumNum']=$sumNum3;
        $data3['CodeNum']=$sumNum3;
        //昨日不良数据总个数和单据数
        $NgNum=(new T_QM_INSPECT_INFO())
            ->where('TEST_STEP','=',2)
            ->where('STATUS','>=',4)
            ->where([['TEST_START_TIME','>=',date('Y-m-d 00:00:00',strtotime('-1 day'))],['TEST_START_TIME','<=',date('Y-m-d 23:59:59',strtotime('-1 day'))]])
            ->where([['TEST_END_TIME','>=',date('Y-m-d 00:00:00',strtotime('-1 day'))],['TEST_END_TIME','<=',date('Y-m-d 23:59:59',strtotime('-1 day'))]])
            ->where('TEST_RESULT','=',2)
            ->field('id')->count();
        $data3['NgSumNum']=$NgNum;
        $data3['NgCodeNum']=$NgNum;
        if($this->request->isAjax()){
            return json(['data1'=>$data1,'data2'=>$data2,'data3'=>$data3],200);
        }

        return view('/FQC',['data1'=>$data1,'data2'=>$data2,'data3'=>$data3]);
    }

}
